﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using iCollect.Chart;
using iCollect.Constants;
using System.Collections;

namespace iCollect.CollectDetail
{
    public partial class YichangshujuChaxun : UserControl
    {
        private LineChartType chart1 = new LineChartType();

        public YichangshujuChaxun()
        {
            InitializeComponent();

            chart1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.panel2.Controls.Add(chart1);
        }

        private void Query_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“cUPDataDataSet.device”中。您可以根据需要移动或移除它。
            //this.normaldataTableAdapter.Fill(this.cUPDataDataSet2.normaldata);

        }

        public void loadDevice()
        {
            //normaldataTableAdapter.GetDataBy();
            //normaldataTableAdapter.FillBy(this.cUPDataDataSet2.normaldata, GlobalConstants.currentDevice);
        }

        private void dataChange(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count == 0)
            {
                chart1.clear();
            }
            else
            {
                DataGridViewCellCollection cells = dataGridView1.SelectedRows[0].Cells;
                string channelID = cells[1].Value.ToString();
                DateTime cTime = (DateTime)cells[2].Value;
                DateTime startTime = DateTime.Parse(dateTimePickerStart.Value.ToShortDateString());
                DateTime endTime = DateTime.Parse(dateTimePickerEnd.Value.AddDays(1).ToShortDateString());
                List<Double> dataList = new List<Double>();
                List<DateTime> dateList = new List<DateTime>();

                DataTable tb = exceptiondataautoTableAdapter.GetDataBy(channelID, startTime, 
                    GlobalConstants.currentDevice, endTime);
                foreach (DataRow row in tb.Rows)
                {
                    dataList.Add(Double.Parse(row[2].ToString()));
                    dateList.Add(DateTime.Parse(row[1].ToString()));
                }
                chart1.InitByChannel(channelID);
                chart1.InitByDateData(dataList, dateList, channelID);

                
            }
        }

        private void string2DoubleArray(string str, List<Double> al)
        {
            string[] strArray = str.Split(',');
            for (int i = 0; i < strArray.Length; i++)
            {
                al.Add(Double.Parse(strArray[i]));
            }
        }

        private void button19_Click(object sender, EventArgs e)
        {
            TimeSpan value = dateTimePickerEnd.Value - dateTimePickerStart.Value;
            if (value.Days > 30)
            {
                alert("时间间隔不能大于30天。");
            }
            //else if (listBoxChannelID.SelectedIndex == -1)
            //{
            //    alert("请选择通道号");
            //}
            else
            {
                queryData();
            }
            
        }

        private void alert(string msg)
        {
            DialogResult MsgBoxResult;
            MsgBoxResult = MessageBox.Show(msg,//对话框的显示内容
            "提示",//对话框的标题
            MessageBoxButtons.OK,//定义对话框的按钮，这里定义了YSE和NO两个按钮
            MessageBoxIcon.Exclamation,//定义对话框内的图表式样，这里是一个黄色三角型内加一个感叹号
            MessageBoxDefaultButton.Button1);//定义对话框的按钮式样
            if (MsgBoxResult == DialogResult.OK)
            {
                //this.Close();
            }
        }

        private void queryData()
        {
            DateTime startTime = DateTime.Parse(dateTimePickerStart.Value.ToShortDateString());
            DateTime endTime = DateTime.Parse(dateTimePickerEnd.Value.AddDays(1).ToShortDateString());
            exceptiondataTableAdapter.FillBy2(this.cupDataDataSet1.exceptiondata, GlobalConstants.currentDevice,
                startTime, endTime);
        }

        private void filterData()
        {
            //this.exceptiondataBindingSource.Filter
            String filterStr;
            filterStr = "time > '" + dateTimePickerStart.Value.ToString() + "' and time <'" +
                dateTimePickerEnd.Value.ToString() + "'";
            int lastIndex;
            if (listBoxChannelType.SelectedItems.Count > 0)
            {
                filterStr += " and (";
                lastIndex = listBoxChannelType.SelectedItems.Count - 1;
                for (int i = 0; i < (lastIndex); i++)
                {
                    filterStr += "type = '" + listBoxChannelType.SelectedItems[i].ToString() + "' or ";
                }
                filterStr += "type = '" + listBoxChannelType.SelectedItems[lastIndex] + "' ";
                filterStr += ")";
            }
            if (listBoxChannelID.SelectedItems.Count > 0)
            {
                filterStr += " and (";
                lastIndex = listBoxChannelID.SelectedItems.Count - 1;
                for (int i = 0; i < (lastIndex); i++)
                {
                    filterStr += "code = '" + listBoxChannelID.SelectedItems[i].ToString() + "' or ";
                }
                filterStr += "code = '" + listBoxChannelID.SelectedItems[lastIndex] + "' ";
                filterStr += ")";
            }
            exceptiondataBindingSource.Filter = filterStr;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            listBoxChannelType.SelectedIndex = -1;
            listBoxChannelID.SelectedIndex = -1;
            exceptiondataBindingSource.Filter = null;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            String filterStr = "";
         
            int lastIndex;
            if (listBoxChannelType.SelectedItems.Count > 0)
            {
                filterStr += "(";
                lastIndex = listBoxChannelType.SelectedItems.Count - 1;
                for (int i = 0; i < (lastIndex); i++)
                {
                    filterStr += "type = '" + listBoxChannelType.SelectedItems[i].ToString() + "' or ";
                }
                filterStr += "type = '" + listBoxChannelType.SelectedItems[lastIndex] + "' ";
                filterStr += ")";
            }
            if (listBoxChannelID.SelectedItems.Count > 0)
            {
                filterStr += " and (";
                lastIndex = listBoxChannelID.SelectedItems.Count - 1;
                for (int i = 0; i < (lastIndex); i++)
                {
                    filterStr += "code = '" + listBoxChannelID.SelectedItems[i].ToString() + "' or ";
                }
                filterStr += "code = '" + listBoxChannelID.SelectedItems[lastIndex] + "' ";
                filterStr += ")";
            }
            exceptiondataBindingSource.Filter = filterStr;
        }

        private void channelTypeChanged(object sender, EventArgs e)
        {
            string chennelType = listBoxChannelType.SelectedItem.ToString();
            if(GlobalConstants.typeChannelMap.ContainsKey(chennelType))
            {
                listBoxChannelID.DataSource = GlobalConstants.typeChannelMap[chennelType];
            }
        }
    }
}
